﻿@inherits BaseComponent

<LoginInfoForm @ref="form" Class="@FormStyle" Model="Model">
    <FormItem Label="" Required Rules="@Context.RuleRequired("UserName")">
        <AntInput @bind-Value="@context.UserName" Placeholder="@Language["UserName"]">
            <Prefix><Icon Type="user" /></Prefix>
        </AntInput>
    </FormItem>
    <FormItem Label="" Required Rules="@Context.RuleRequired("Password")">
        <InputPassword @bind-Value="@context.Password" Placeholder="@Language["Password"]">
            <Prefix><Icon Type="lock" /></Prefix>
        </InputPassword>
    </FormItem>
    @if (Stations != null && Stations.Count > 0)
    {
        <FormItem Label="" Required Rules="@Context.RuleRequired("Station")">
            <AntSelect @bind-Value="@context.Station" DataSource="Stations" Placeholder="@Language["Station"]">
                <PrefixIcon><Icon Type="control" Style="padding:0 9px 0 7px;" /></PrefixIcon>
            </AntSelect>
        </FormItem>
    }
    else if (IsCaptcha)
    {
        <FormItem Label="" Required Rules="@Context.RuleRequired("Captcha")">
            <AntCaptcha @ref="captcha" @bind-Value="@context.Captcha" Placeholder="@Language["Captcha"]">
                <Prefix><Icon Type="check" /></Prefix>
            </AntCaptcha>
        </FormItem>
    }
    <FormItem>
        <Switch @bind-Value="@context.Remember" /> @Language["Login.RememberUser"]
    </FormItem>
    <FormItem>
        <AntButton Name="@Language["Login"]" Type="@ButtonType.Primary" Block OnClick="OnUserLogin" />
    </FormItem>
</LoginInfoForm>

@code {
    private LoginInfoForm form;
    private AntCaptcha captcha;
    private bool IsCaptcha => Context.System != null && Context.System.IsLoginCaptcha;
    private string FormStyle => IsCaptcha || Stations != null ? "" : "kui-nocaptcha";

    [Parameter] public List<string> Stations { get; set; }
    [Parameter] public LoginFormInfo Model { get; set; }
    [Parameter] public Func<Task> OnLogin { get; set; }

    private async Task OnUserLogin(MouseEventArgs args)
    {
        if (!form.Validate())
            return;

        if (IsCaptcha)
        {
            if (!captcha.Validate(out string message))
            {
                await UI.Toast(message, StyleType.Error);
                return;
            }
        }

        Model.IsMobile = Context.IsMobile;
        await OnLogin?.Invoke();
    }
}